/*
 * Copyright 2011 Clean GWT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleangwt.validation.client;

import com.cleangwt.validation.client.constraints.FutureValidation;
import com.cleangwt.validation.client.constraints.MaxValidation;
import com.cleangwt.validation.client.constraints.MinValidation;
import com.cleangwt.validation.client.constraints.NotNullValidation;
import com.cleangwt.validation.client.constraints.PastValidation;
import com.cleangwt.validation.client.constraints.PatternValidation;
import com.cleangwt.validation.client.constraints.RemoteValidation;
import com.cleangwt.validation.client.constraints.SizeValidation;
import com.google.gwt.user.client.ui.HasValue;

/**
 * The {@link ValidationContextBuilder} for building {@link ValidationContext} with single input
 * field.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class SingleFieldBuilder extends ValidationContextBuilder {

  /**
   * Creates a single-field context builder with the given input.
   * 
   * @param input the input.
   */
  public SingleFieldBuilder(HasValue<?> input) {
    super(new HasValue<?>[] {input});
    assert input != null : "The input must not be null.";
  }

  /**
   * Adds a {@link Validation} to the {@link ValidationContext}.
   * 
   * @param validation the validation
   * @return this for continuously building
   */
  public SingleFieldBuilder add(Validation validation) {
    super.add(validation);
    return this;
  }

  /**
   * Sets the binding groups for the next {@link Validation}s to add.
   * 
   * @param groups the groups
   * @return this for continuously building
   */
  public SingleFieldBuilder forGroups(String... groups) {
    return (SingleFieldBuilder) super.forGroups(groups);
  }

  /**
   * Adds a {@link FutureValidation} to the {@link ValidationContext}.
   * 
   * @return this for continuously building
   */
  public SingleFieldBuilder future() {
    return add(new FutureValidation(ValidationMessages.Impl.future()));
  }

  /**
   * Adds a {@link MaxValidation} to the {@link ValidationContext}.
   * 
   * @param value the max value to constraint
   * @return this for continuously building
   */
  public SingleFieldBuilder max(long value) {
    return add(new MaxValidation(value, ValidationMessages.Impl.max(value)));
  }

  /**
   * Adds a {@link MinValidation} to the {@link ValidationContext}.
   * 
   * @param value the min value to constraint
   * @return this for continuously building
   */
  public SingleFieldBuilder min(long value) {
    return add(new MinValidation(value, ValidationMessages.Impl.min(value)));
  }

  /**
   * Adds a {@link NotNullValidation} to the {@link ValidationContext}.
   * 
   * @return this for continuously building
   */
  public SingleFieldBuilder notNull() {
    return add(NotNullValidation.DEFAULT);
  }

  /**
   * Adds a {@link PastValidation} to the {@link ValidationContext}.
   * 
   * @return this for continuously building
   */
  public SingleFieldBuilder past() {
    return add(new PastValidation(ValidationMessages.Impl.past()));
  }

  /**
   * Adds a {@link PattenValidator} to the {@link ValidationContext}.
   * 
   * @param regex the regular expression
   * @return this for continuously building
   */
  public SingleFieldBuilder pattern(String regex) {
    return add(new PatternValidation(regex, ValidationMessages.Impl.pattern(regex)));
  }

  /**
   * Adds a {@link RemoteValidation} to the {@link ValidationContext}.
   * 
   * @param url the url of remote service
   * @param message the default error message for the remote service response an empty string
   * @return this for continuously building
   */
  public SingleFieldBuilder remote(String url, String message) {
    return add(new RemoteValidation(url, message));
  }

  /**
   * Adds a {@link SizeValidation} to the {@link ValidationContext}.
   * 
   * @return this for continuously building
   */
  public SingleFieldBuilder size(long min, long max) {
    return add(new SizeValidation(min, max, ValidationMessages.Impl.size(min, max)));
  }
}
